package com.wondream.myframework.app.common.config.datasource;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author wondream
 * @version 1.0
 * @create 2022/11/16 9:25 PM
 */
@Aspect
@Component
@Order(0)
public class DynamicDataSourceAspect {
    /**
     * 一个service方法为一个事务，事务内部使用同一个数据源
     */
    @Before("execution(public * com.wondream.myframework.app.service.*.*(..))")
    public void beforeAdvice(JoinPoint pjp){
        String methodName = pjp.getSignature().getName();
        //1、从库
        if(StringUtils.startsWithAny(methodName, "query", "find", "get", "select")){
            DynamicDataSourceHolder.markSlave();
            return ;
        }
        //2、主库
        DynamicDataSourceHolder.markMaster();

    }
}
